// 1.12 June 21, 1995
// Fixed the phoneAbbrs. It seems that the docs are wrong and German
// Newts return 3 for getLanguageEnvironment().

// 1.11 June 19, 1995
// Moved array lookup out of drawing loop.

// 1.1 June 18, 1995
// Added German and French abbreviations.

// 1.02 June 12, 1995
// Added upcase() to ensure that index on left of screen is upper-case.

// 1.01 June 9, 1995

// Copyright  1995 Matthew Dixon Cowles and his licensors. All rights reserved.
// For license information see the readme file.


constant kMyRootSlot:='|NOH:MONDO|;
constant kShortAppName:="NOH";

constant kInitialLeft:=1;
constant kInitialRight:=12;
constant kNameLeft:=15;
constant kNameRight:=207;
constant kPhoneRight:=221;
constant kPhoneAbbrLeft:=220;
constant kPhoneAbbrRight:=235;
constant kNameMarginRight:=4;

defConst('kPhoneAbbrs,
  [
    // English
    {
    phone:"",
    homePhone:"H",
    workPhone:"W",
    carPhone:"C",
    mobilePhone:"M",
    beeperPhone:"B",
    otherPhone:"X",
    faxPhone:"F",
    },

    // French
    {
    phone:"T",
    homePhone:"D",
    workPhone:"B",
    carPhone:"V",
    mobilePhone:"P",
    beeperPhone:"BI",
    otherPhone:"A",
    faxPhone:"F",
    },

    // Don't know what a 2 is.
    {
    },

    // German
    {
    phone:"",
    homePhone:"P",
    workPhone:"B",
    carPhone:"A",
    mobilePhone:"M",
    beeperPhone:"M",
    otherPhone:"X",
    faxPhone:"F",
    },
  ]);

defConst('kMyCfOverDraw,func(cursor)
  begin
  local myText,myShape;
  local compnayText,nameText;
  local lineTop,lineBottom;
  local spaceForName;
  local phoneText;
  local lineCount:=0;
  local prevStartChar:=NIL;
  local thisEntry:=cursor:entry();
  local myFont:=drawFont;
  local myNumLines:=numLines;
  local myLineHeight:=lineHeight;

  local myLanguage:=call kGetLanguageEnvironmentFunc with ();
  // We only do English (0), French (1), and German(3).
  if myLanguage<0 or myLanguage>3 or myLanguage=2 then
    myLanguage:=0;

  // Avoid doing the array lookup in the loop.
  local myPhoneAbbrs:=kPhoneAbbrs[myLanguage];

  while thisEntry<>NIL and lineCount<=myNumLines do
    begin
    lineTop:=(lineCount*myLineHeight)-1;
    lineBottom:=((lineCount+1)*myLineHeight)-1;

    if prevStartChar=NIL or not beginsWith(thisEntry.sortOn,prevStartChar) then
      begin
      prevStartChar:=upCase(subStr(thisEntry.sortOn,0,1));
      myShape:=makeRect(kInitialLeft,lineTop+2,kInitialRight,lineBottom+2);
      self:drawShape(myShape,{fillPattern:vfBlack});
      myShape:=makeText(prevStartChar,kInitialLeft,lineTop,kInitialRight,lineBottom);
      self:drawShape(myShape,{font:myFont,justification:'center,transferMode:modeXOr});
      end;

    if thisEntry.phones<>NIL and length(thisEntry.phones)>0 then
      begin
      phoneText:=thisEntry.phones[0];
      myShape:=makeText(phoneText,kNameLeft,lineTop,kPhoneRight,lineBottom);
      self:drawShape(myShape,{font:myFont,justification:'right,transferMode:modeCopy});
      myShape:=makeText(myPhoneAbbrs.(classOf(phoneText)),kPhoneAbbrLeft,lineTop,kPhoneAbbrRight,lineBottom);
      spaceForName:=kNameRight-(strFontWidth(phoneText,myFont)+kNameMarginRight);
      self:drawShape(myShape,{font:myFont,justification:'center});
      end;
    else
      spaceForName:=kNameRight-kNameMarginRight;

    if classOf(thisEntry.sortOn)='company then
      begin
      companyText:=clone(thisEntry.company);
      if thisEntry.name<>NIL then
        begin
        nameText:=trimString(thisEntry.name.first&&thisEntry.name.last);
        if strLen(nameText)<>0 then
          nameText:="("&nameText&")";
        end;
      else
        nameText:="";
      while strFontWidth(trimString(companyText&&nameText),myFont)>spaceForName do
        begin
        if not strEqual(nameText,"") then
          nameText:=subStr(nameText,0,strLen(nameText)-1);
        else
          companyText:=subStr(companyText,0,strLen(companyText)-1);
        end;
      myText:=trimString(companyText&&nameText);       
      end;
    else
      begin
      myText:=trimString(thisEntry.name.last&","&&thisEntry.name.first);
      while strFontWidth(myText,myFont)>spaceForName do
        myText:=subStr(myText,0,strLen(myText)-1);
      end;

    myShape:=makeText(myText,kNameLeft,lineTop,kPhoneRight,lineBottom);

    self:drawShape(myShape,{font:myFont});

    cursor:next();
    thisEntry:=cursor:entry();
    lineCount:=lineCount+1;
    end;
  numDrawn:=lineCount;
  end);

defConst('kMyNotify,func(msg)
  begin
  getRoot():notify(kNotifyQAlert,ensureInternal(kShortAppName),ensureInternal(msg));
  end);

installScript:=func(partFrame,removeFrame)
  begin

  // Do some rudimentray checking.
  if not getroot().cardfile exists or
    not getroot().cardfile._proto exists or
    not getRoot().cardFile._proto.allocateContext exists or
    classOf(getRoot().cardFile.allocateContext)<>'array or
    length(getRoot().cardFile.allocateContext)<>12 or
    not getRoot().cardFile.allocateContext[7].cfOverDraw exists or
    classOf(getRoot().cardFile.allocateContext[7].cfOverDraw)<>'CFunction then
    begin
    call kMyNotify with ("NOH can't find the right place to install the hack. You may as well remove it.");
    return NIL;
    end;

	getRoot().(kMyRootSlot):={oldOverDrawFunc:getRoot().cardFile._proto.allocateContext[7].cfOverDraw};
  getRoot().cardFile.(ensureInternal('_proto)):=clone(getRoot().cardFile._proto);
  getRoot().cardFile._proto.(ensureInternal('allocateContext)):=clone(getRoot().cardFile._proto.allocateContext);
  getRoot().cardFile._proto.allocateContext[7]:=clone(getRoot().cardFile._proto.allocateContext[7]);
  getRoot().cardFile._proto.allocateContext[7].cfOverDraw:=kMyCfOverDraw;
  end;

removeScript:=func(removeFrame)
  begin
  if getRoot().(kMyRootSlot) exists and
    getRoot().(kMyRootSlot).oldOverDrawFunc exists and
    classOf(getRoot().(kMyRootSlot).oldOverDrawFunc)='CFunction then
    getRoot().cardFile._proto.allocateContext[7].cfOverDraw:=getRoot().(kMyRootSlot).oldOverDrawFunc;
	removeSlot(getRoot(),(kMyRootSlot));
  end;
